package com.macbulldev.poc.testgin.domain;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class PolicyForm {
	
	private static final Logger LOGGER = Logger.getLogger(PolicyForm.class);
	
	@NotEmpty(message = "Cannot Be Empty")
    private String displayName;
	
	@NotEmpty(message = "Cannot Be Empty")
    private String importName;
	
	@NotEmpty(message = "Cannot Be Empty")
    private String abbrName;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "policyForm")
    private Set<LiabilityAmount> liabilityAmounts;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.TrialGroup.class)
    @javax.persistence.JoinColumn(name="trialGroupId")
    private com.macbulldev.poc.testgin.domain.TrialGroup trialGroup;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.StateOrProvince.class)
    @javax.persistence.JoinColumn(name="stateOrProvinceId")
    private com.macbulldev.poc.testgin.domain.StateOrProvince stateOrProvince;
    
    
    private String createUsername;
    private String updateUsername;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            PolicyForm attached = this.entityManager.find(PolicyForm.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        PolicyForm merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new PolicyForm().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countPolicyForms() {    
        return (Long) entityManager().createQuery("select count(o) from PolicyForm o").getSingleResult();        
    }

	public static List<PolicyForm> findAllPolicyForms() {    
        return entityManager().createQuery("select o from PolicyForm o").getResultList();        
    }

	public static PolicyForm findPolicyForm(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of PolicyForm");        
        return entityManager().find(PolicyForm.class, id);        
    }

	public static List<PolicyForm> findPolicyFormEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from PolicyForm o where o.createUsername != '' order by o.displayName asc").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public static List<PolicyForm> findAllDistinctPolicyForms() {
        return entityManager().createQuery("select o from PolicyForm o where o.createUsername != '' order by o.displayName asc").getResultList();        
    }
	
	@Transactional
	public static void deletePolicyFormByTrialGroup(PolicyForm policyForm)
	{
		
		EntityManager entityManager = new PolicyForm().entityManager;
		String hql = "delete from PolicyForm pf where upper(trim(pf.displayName))=upper(trim(?)) and upper(trim(pf.importName))=upper(trim(?)) and upper(trim(pf.abbrName))=upper(trim(?))  and pf.trialGroup.id = ?";
		Query q = null;
		
		
		try{
    		
    		q = entityManager.createQuery(hql);
    		
    	}
    	catch(Exception ex)
    	{
    		LOGGER.error("Error:  ",ex);
    	}
    	
    	q.setParameter(1,policyForm.getDisplayName());
    	q.setParameter(2,policyForm.getImportName());
    	q.setParameter(3,policyForm.getAbbrName());
    	q.setParameter(4,policyForm.getTrialGroup().getId());
    	q.executeUpdate();
    	
		
	}
	
	

	public String getDisplayName() {    
        return this.displayName;        
    }

	public void setDisplayName(String displayName) {    
        this.displayName = displayName;        
    }

	public String getImportName() {    
        return this.importName;        
    }

	public void setImportName(String importName) {    
        this.importName = importName;        
    }

	public String getAbbrName() {    
        return this.abbrName;        
    }

	public void setAbbrName(String abbrName) {    
        this.abbrName = abbrName;        
    }

	public TrialGroup getTrialGroup() {    
        return this.trialGroup;        
    }

	public void setTrialGroup(TrialGroup trialGroup) {    
        this.trialGroup = trialGroup;        
    }

	public StateOrProvince getStateOrProvince() {    
        return this.stateOrProvince;        
    }

	public void setStateOrProvince(StateOrProvince stateOrProvince) {    
        this.stateOrProvince = stateOrProvince;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("DisplayName: ").append(getDisplayName()).append(", ");        
        sb.append("ImportName: ").append(getImportName()).append(", ");        
        sb.append("AbbrName: ").append(getAbbrName()).append(", ");        
        sb.append("TrialGroup: ").append(getTrialGroup()).append(", ");        
        sb.append("StateOrProvince: ").append(getStateOrProvince()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        return sb.toString();        
    }
}
